﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using VideoClassLibrary.Model;
using VideoClassLibrary.BusinessLogicLayer;
public partial class ClassRoomManagement_SchoolAdminSearcherRoomList : AdminUserBase
{
    ClassRoomModule room = new ClassRoomModule();
    public string PublishedStatus;
    public string exeJs = string.Empty;
    protected void Page_Load(object sender, EventArgs e)
    {
        PublishedStatus = Request.QueryString["IsPublished"];
        if (!IsPostBack)
        {
            RoomPublished();
            Bind();
        }
        ClientScript.RegisterStartupScript(typeof(string), "exejs", "<script>" + exeJs + "</script>");
    }
    public void Bind()
    {
        DataSet ds = new DataSet();
        ClassRoomModule cm = new ClassRoomModule();
        string str = " and IsPublished=1";
        exeJs = "swithTab('tab1')";
        if (!string.IsNullOrEmpty(PublishedStatus) && int.Parse(PublishedStatus) == 0)
        {
            str = "  and  IsPublished=0";
            exeJs = "swithTab('tab2')";
        }
        if (this.SchoolID == 0)
        {
            ds = cm.GetAllRoom();
        }
        else
        {
            ds = cm.GetRoomListByWhereAndSID(str, this.SchoolID);
        }
        Repeater1.DataSource = getPage(ds);
        Repeater1.DataBind();
    }
    public string GetSchNameByID(int schid)
    {
        TeacherModule ta = new TeacherModule();
        string name = ta.GetSchNameByID(schid);
        return name;
    }
    protected void BtCheck_Click(object sender, EventArgs e)
    {
        string name = TxtCheckRoom.Text.Trim();
        if (name == "" || name == null)
        {
            Bind();
        }
        else
        {
            CheckBind(name);
        }
    }
    public void CheckBind(string name)
    {
        ClassRoomModule cm = new ClassRoomModule();
        DataSet ds = new DataSet();
        if (this.SchoolID == 0)
        {
            ds = cm.QueryByRoomname(name);
        }
        else
        {
            string where=" and  VR_roomName  like '%" + name + "%'";
            ds = cm.GetRoomListByWhereAndSID(where, this.SchoolID);
        }
        Repeater1.DataSource = getPage(ds);
        Repeater1.DataBind();
    }
    
    protected void pager_PageChanged(object sender, EventArgs e)
    {
        string name = TxtCheckRoom.Text.Trim();
        if (name == "" || name == null)
        {
            Bind();
        }
        else
        {
            CheckBind(name);
        }
    }
    public PagedDataSource getPage(DataSet ds)
    {
        this.pager.RecordCount = ds.Tables[0].Rows.Count;
        PagedDataSource pds = new PagedDataSource();
        pds.DataSource = ds.Tables[0].DefaultView;
        pds.AllowPaging = true;
        pds.CurrentPageIndex = pager.CurrentPageIndex - 1;
        pds.PageSize = pager.PageSize;
        return pds;
    }
    #region
    protected void btnTeachers_Click(object sender, EventArgs e)
    {
        Response.Redirect(".SchoolAdminSearcherRoomList.aspx");
    }

    protected void repNews_ItemCommand(object source, RepeaterCommandEventArgs e)
    {

        if (e.CommandName.ToLower() == "setispublished")
        {
            string argValue = e.CommandArgument.ToString();
            room.UpdateIsPublished(this.SchoolID,argValue.Split('|')[1] == "0" ? 1 : 0,int.Parse(argValue.Split('|')[0]));
        }
        Response.Redirect("SchoolAdminSearcherRoomList.aspx");

    }
    protected void repNews_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
        {
            DataRowView drv = (DataRowView)e.Item.DataItem;

            string IsPublished = drv["IsPublished"].ToString();
            int schoolid = this.CurrentUser.SchoolID;
            LinkButton btnIsPublished = (LinkButton)e.Item.FindControl("btnIsPublished");
            btnIsPublished.Text = (IsPublished == "1" ? "禁用" : "发布");
             LinkButton lbtnDel = e.Item.FindControl("btnDelete") as LinkButton;
             if (IsPublished == "1")
             {
                 lbtnDel.Visible = false;
             }
             else
             {
                 lbtnDel.Visible = true;
             }
        }
    }
    /// <summary>
    /// 根据选择删除数据
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnDelete_Command(object sender, CommandEventArgs e)
    {
        int id = int.Parse(e.CommandArgument.ToString());
        room.RoomDelete(id);
        Response.Redirect("SchoolAdminSearcherRoomList.aspx");
    }
    /// <summary>
    /// 修改新闻
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    public void btnUpdate_Click(object sender, CommandEventArgs e)
    {
        Response.Redirect("UpdateRoom.aspx?RID=" + e.CommandArgument.ToString());
    }

#endregion
    ////启用
    //protected void lbtnpublished_Click(object sender, EventArgs e)
    //{

    //    RoomPublished();
    //}
    //状态被启用
    private void RoomPublished()
    {
        string str = " and IsPublished=1";
        V_room rm = new V_room();
        ClassRoomModule crm = new ClassRoomModule();
        DataSet ds = new DataSet();
        if (this.SchoolID == 0)
        {

            ds = crm.GetAllRoom();
        }
        else
        {
            ds = crm.GetRoomListByWhereAndSID(str, SchoolID);
        }
        Repeater1.DataSource = getPage(ds);
        Repeater1.DataBind();
    }
    ////禁用
    //protected void lbtnNot_Click(object sender, EventArgs e)
    //{
    //    string str = " and IsPublished=0";
    //    V_room rm = new V_room();
    //    ClassRoomModule crm = new ClassRoomModule();
    //    DataSet ds = new DataSet();
    //    if (this.SchoolID == 0)
    //    {
    //        ds = crm.GetAllRoom();
    //    }
    //    else
    //    {
    //        ds = crm.GetRoomListByWhereAndSID(str, SchoolID);
    //    }
    //    Repeater1.DataSource = getPage(ds);
    //    Repeater1.DataBind();
    //}
}

